package com.cold.demo.oos;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.*;

import javax.annotation.PreDestroy;
import java.io.Closeable;
import java.util.List;

public class OSSBucket implements Closeable {


    private OSS ossClient;

    private OSS getOSSClient() {
        if (ossClient != null) {
            return ossClient;
        }
        synchronized (this) {
            if (ossClient != null) {
                return ossClient;
            }
            ossClient = new OSSClientBuilder().build(OSSConfig.endpoint, OSSConfig.accessKeyId, OSSConfig.accessKeySecret);
            return ossClient;
        }
    }


    public static void main(String[] args) {
        OSSBucket ossBucket = new OSSBucket();

        ossBucket.createBucket("cold-test3");

        System.out.println(new OSSBucket().listBuckets());

        ossBucket.close();


    }

    /**
     * 判断是否存在该 bucketName
     * @param bucketName
     * @return
     */
    public boolean doesBucketExist(String bucketName) {
        return getOSSClient().doesBucketExist(bucketName);
    }

    /**
     * 获取该 bucketName 的所属地域
     * @return
     */
    public String getBucketLocation(String bucketName){
        return getOSSClient().getBucketLocation(bucketName);
    }

    public BucketInfo getBucketInfo(String bucketName){
        return getOSSClient().getBucketInfo(bucketName);
    }

    public List<com.aliyun.oss.model.Bucket> listBuckets() {
        return getOSSClient().listBuckets();
    }

    public BucketList listBuckets(String prefix, String marker, Integer maxKeys) {
        return getOSSClient().listBuckets(prefix, marker, maxKeys);
    }

    public BucketList listBuckets(ListBucketsRequest listBucketsRequest) {
        return getOSSClient().listBuckets(listBucketsRequest);
    }



    // ==============================================删除开始=============================================================
    public void deleteBucket(String bucketName) {
        OSS oss = getOSSClient();
        oss.deleteBucket(bucketName);
    }

    public void deleteBucketCname(String bucketName, String domain){
        OSS oss = getOSSClient();
        oss.deleteBucketCname(bucketName,domain);
    }
    // ==============================================删除开始=============================================================



    // ==============================================新增开始=============================================================

    public void createBucket(String bucketName) {
        this.createBucket(bucketName, StorageClass.Standard, DataRedundancyType.LRS, CannedAccessControlList.Private, null);
    }

    /**
     * @param bucketName              bucket名称
     * @param storageClass            此处以设置存储空间的存储类型为标准存储为例介绍  默认为：标准存储
     * @param dataRedundancyType      数据容灾类型默认为本地冗余存储，即DataRedundancyType.LRS。如果需要设置数据容灾类型为同城冗余存储，请设置为 DataRedundancyType.ZRS 。
     * @param cannedAccessControlList 设置存储空间的权限  默认为私有
     * @param locationConstraint      数据的存储中心
     */
    public void createBucket(String bucketName, StorageClass storageClass, DataRedundancyType dataRedundancyType, CannedAccessControlList cannedAccessControlList, String locationConstraint) {
        CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);

        if (storageClass != null) {
            createBucketRequest.setStorageClass(storageClass);
        }
        if (dataRedundancyType != null) {
            createBucketRequest.setDataRedundancyType(dataRedundancyType);
        }
        if (cannedAccessControlList != null) {
            createBucketRequest.setCannedACL(cannedAccessControlList);
        }

        if (locationConstraint != null && !locationConstraint.isEmpty()) {
            createBucketRequest.setLocationConstraint(locationConstraint);
        }

        getOSSClient().createBucket(createBucketRequest);
    }
    // ==============================================新增结束=============================================================


    @Override
    @PreDestroy
    public void close() {
        if (ossClient!=null){
            ossClient.shutdown();
        }
    }
}
